package com.aiyige.page.my.customer;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.BaseFragment;
import com.aiyige.page.my.customer.fragment.CustomerBaseDataPage;
import com.aiyige.page.my.customer.fragment.CustomerOrderRecordDataPage;
import com.aiyige.page.my.customer.model.CustomerEntity;
import com.aiyige.track.annotation.Track;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 客户管理筛选
 * Created by xiachao on 2018/3/26.
 */
@Track("客户详情页")
@Route(path = ARouterConfig.CustomerDetailPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class CustomerDetailPage extends BaseActivity {

    @Autowired
    CustomerEntity customer;
    @BindView(R.id.mainVp)
    ViewPager mainVp;
    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;

    MyPagerAdapter pagerAdapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_customer_detail);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        if (pagerAdapter == null) {
            pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        }
        mainVp.setAdapter(pagerAdapter);
        mainVp.setOffscreenPageLimit(pagerAdapter.getCount());
        tabLayout.setViewPager(mainVp);
        tabLayout.setTextBold(1);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 0) {
                    //刷新 基本信息
                    ((CustomerBaseDataPage)pagerAdapter.getItem(0)).setCustomerEntity(customer);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        fillViews();
    }


    private void fillViews() {
        ((CustomerBaseDataPage)pagerAdapter.getItem(0)).setCustomerEntity(customer);
        ((CustomerOrderRecordDataPage)pagerAdapter.getItem(1)).setCustomerEntity(customer);
    }


    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }


    public class MyPagerAdapter extends FragmentPagerAdapter {
        List<BaseFragment> pageList = new LinkedList<>();


        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
            pageList.add(CustomerBaseDataPage.newInstance());
            pageList.add(CustomerOrderRecordDataPage.newInstance());
        }

        @Override
        public Fragment getItem(int position) {
            return pageList.get(position);
        }

        @Override
        public int getCount() {
            return pageList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getString(position==0?R.string.base_data:R.string.order_record);
        }



    }


}
